﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Emotiv;
using System.IO;

namespace Epoc_project
{
    public partial class Form1 : Form
    {
        
        static Single yesThreshhold = 0F, noThreshhold = 0F;
        static EdkDll.EE_CognitivAction_t yesTrigger = EdkDll.EE_CognitivAction_t.COG_PULL, noTrigger = EdkDll.EE_CognitivAction_t.COG_PUSH;
        static Profile[] profiles;
        private static int interval = 1000;
        EmoEngine EE;
        Timer t;
        static string ip = "127.0.0.1";
        static ushort port = 3008;
        public Form1()
        {
            InitializeComponent();
            loadSettings();
            initEpoc();
            initTimer();
        }

        private void loadSettings(){
            if (File.Exists("settings.txt"))
            {
                string[] settings = File.ReadAllLines("settings.txt");
                foreach (string line in settings)
                {
                    if (!line.StartsWith("#"))
                    {
                        if (line.StartsWith("yes_trigger"))
                        {
                            
                            string[] s = line.Split(new[]{' '}, StringSplitOptions.RemoveEmptyEntries);
                            if (s.Length == 2)
                            {
                                comboBox1.Text = s[1];
                            }
                        }
                        else if (line.StartsWith("no_trigger"))
                        {
                            string[] s = line.Split(new[]{' '}, StringSplitOptions.RemoveEmptyEntries);
                            if (s.Length == 2)
                            {
                                comboBox2.Text = s[1];
                            }
                        }
                        else if (line.StartsWith("yes_threshold"))
                        {
                            string[] s = line.Split(new[]{' '}, StringSplitOptions.RemoveEmptyEntries);
                            if (s.Length == 2)
                            {
                                trackBarSensitivityYes.Value = int.Parse(s[1]);
                            }
                        }
                        else if (line.StartsWith("no_threshold"))
                        {
                            string[] s = line.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                            if (s.Length == 2)
                            {
                                trackBarSensitivityNo.Value = int.Parse(s[1]);
                            }
                        }
                        else if (line.StartsWith("ip"))
                        {
                            string[] s = line.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                            if (s.Length == 2)
                            {
                                ip = s[1];
                            }
                        }
                        else if (line.StartsWith("port"))
                        {
                            string[] s = line.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                            if (s.Length == 2)
                            {
                                port = ushort.Parse(s[1]);
                            }
                        }
                        else
                        {
                            MessageBox.Show("Invalid settings file, using default settings");
                            comboBox1.Text = "PULL";
                            comboBox2.Text = "PUSH";
                            trackBarSensitivityYes.Value = 0;
                            trackBarSensitivityNo.Value = 0;
                            ip = "127.0.0.1";
                            port = 3008;
                            break;
                        }
                    }
                }
            }
            else File.Create("settings.txt");
        }
        private void initTimer()
        {
            t = new Timer();
            t.Interval = interval;
            t.Tick += new EventHandler(t_tick);
            t.Start();
        }
        void initEpoc()
        {
            try
            {
                EE = EmoEngine.Instance;
                EE.CognitivEmoStateUpdated += new EmoEngine.CognitivEmoStateUpdatedEventHandler(engine_CognitivEmoStateUpdated);
                EE.RemoteConnect(ip, port); 
                //EE.RemoteConnect("127.0.0.1", 1726);
                //EE.Connect();
            }
            catch (Emotiv.EmoEngineException e)
            {
                Console.WriteLine(e.Message);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
        }

        void t_tick(object sender, EventArgs e)
        {
            EE.ProcessEvents(interval);
        }
        void engine_CognitivEmoStateUpdated(object sender, EmoStateUpdatedEventArgs e)
        {
            EmoState es = e.emoState;
            if (es.CognitivIsActive())
            {

                EdkDll.EE_CognitivAction_t cogAction = es.CognitivGetCurrentAction();
                if (cogAction == yesTrigger || cogAction == noTrigger)
                {
                    Single power = es.CognitivGetCurrentActionPower();
                    verticalProgressBar1.Value = (int)(power * 100);
                    if (cogAction == yesTrigger)
                    {
                        labelState.Text = "Yes";
                        if (power >= yesThreshhold)
                        {
                            pbResponse.BackColor = Color.Green;
                        }
                        else pbResponse.BackColor = Color.Transparent;
                    }
                    else if (cogAction == noTrigger)
                    {
                        labelState.Text = "No";
                        if (power >= noThreshhold)
                        {
                            pbResponse.BackColor = Color.Red;
                        }
                        else pbResponse.BackColor = Color.Transparent;
                    }
                    //else throw new Exception("Detected cog-action is neither set as yes nor no");
                }
                else
                {
                    labelState.Text = "Neutral";
                    pbResponse.BackColor = Color.Transparent;
                    verticalProgressBar1.Value = 0;
                }
            }
            else
            {
                pbResponse.BackColor = Color.Orange;
            }

        }

        private void comboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (!(comboBox1.SelectedIndex == comboBox2.SelectedIndex))
            {
                switch (comboBox1.SelectedIndex)
                {
                    case 0:
                        yesTrigger = EdkDll.EE_CognitivAction_t.COG_PUSH;
                        break;
                    case 1:
                        yesTrigger = EdkDll.EE_CognitivAction_t.COG_PULL;
                        break;
                    case 2:
                        yesTrigger = EdkDll.EE_CognitivAction_t.COG_LIFT;
                        break;
                    case 3:
                        yesTrigger = EdkDll.EE_CognitivAction_t.COG_DROP;
                        break;
                    case 4:
                        yesTrigger = EdkDll.EE_CognitivAction_t.COG_LEFT;
                        break;
                    case 5:
                        yesTrigger = EdkDll.EE_CognitivAction_t.COG_RIGHT;
                        break;
                    case 6:
                        yesTrigger = EdkDll.EE_CognitivAction_t.COG_ROTATE_LEFT;
                        break;
                    case 7:
                        yesTrigger = EdkDll.EE_CognitivAction_t.COG_ROTATE_RIGHT;
                        break;
                    case 8:
                        yesTrigger = EdkDll.EE_CognitivAction_t.COG_ROTATE_CLOCKWISE;
                        break;
                    case 9:
                        yesTrigger = EdkDll.EE_CognitivAction_t.COG_ROTATE_COUNTER_CLOCKWISE;
                        break;
                    case 10:
                        yesTrigger = EdkDll.EE_CognitivAction_t.COG_ROTATE_FORWARDS;
                        break;
                    case 11:
                        yesTrigger = EdkDll.EE_CognitivAction_t.COG_ROTATE_REVERSE;
                        break;
                    case 12:
                        yesTrigger = EdkDll.EE_CognitivAction_t.COG_DISAPPEAR;
                        break;
                }
                switch (comboBox2.SelectedIndex)
                {
                    case 0:
                        noTrigger = EdkDll.EE_CognitivAction_t.COG_PUSH;
                        break;
                    case 1:
                        noTrigger = EdkDll.EE_CognitivAction_t.COG_PULL;
                        break;
                    case 2:
                        noTrigger = EdkDll.EE_CognitivAction_t.COG_LIFT;
                        break;
                    case 3:
                        noTrigger = EdkDll.EE_CognitivAction_t.COG_DROP;
                        break;
                    case 4:
                        noTrigger = EdkDll.EE_CognitivAction_t.COG_LEFT;
                        break;
                    case 5:
                        noTrigger = EdkDll.EE_CognitivAction_t.COG_RIGHT;
                        break;
                    case 6:
                        noTrigger = EdkDll.EE_CognitivAction_t.COG_ROTATE_LEFT;
                        break;
                    case 7:
                        noTrigger = EdkDll.EE_CognitivAction_t.COG_ROTATE_RIGHT;
                        break;
                    case 8:
                        noTrigger = EdkDll.EE_CognitivAction_t.COG_ROTATE_CLOCKWISE;
                        break;
                    case 9:
                        noTrigger = EdkDll.EE_CognitivAction_t.COG_ROTATE_COUNTER_CLOCKWISE;
                        break;
                    case 10:
                        noTrigger = EdkDll.EE_CognitivAction_t.COG_ROTATE_FORWARDS;
                        break;
                    case 11:
                        noTrigger = EdkDll.EE_CognitivAction_t.COG_ROTATE_REVERSE;
                        break;
                    case 12:
                        noTrigger = EdkDll.EE_CognitivAction_t.COG_DISAPPEAR;
                        break;
                }
            }
            else
            {
                if (((ComboBox)sender).SelectedIndex == 0)
                {
                    ((ComboBox)sender).SelectedIndex = 1;
                }
                else ((ComboBox)sender).SelectedIndex = 0;

                MessageBox.Show("yes and no must have different triggers!");
            }
            try
            {
                EE.CognitivSetActiveActions((uint)0, (uint)(yesTrigger | noTrigger));
            }
            catch (Exception ex) { }
        }

        private void trackBarSensitivityYes_Scroll(object sender, EventArgs e)
        {
            yesThreshhold = (Single)(trackBarSensitivityYes.Value / 10F);
            Console.WriteLine(yesThreshhold.ToString());
        }

        private void trackBarSensitivittNo_Scroll(object sender, EventArgs e)
        {
            noThreshhold = (Single)(trackBarSensitivityNo.Value / 10F);
            Console.WriteLine(noThreshhold.ToString());
        }

        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {
            try
            {
                EE.Disconnect();
            }
            catch (Exception ex)
            {

            };
        }
    }
}
